<?php
/**
 * Created by PhpStorm.
 * User: 68008000
 * Date: 2017/12/7
 * Time: 14:44
 */
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
namespace app\index\controller;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

/**
 * 系统邮件发送函数
 * @param string $tomail 接收邮件者邮箱
 * @param string $name 接收邮件者名称
 * @param string $subject 邮件主题
 * @param string $body 邮件内容
 * @param string $attachment 附件列表
 * @return boolean
 * @author static7 <static7@qq.com>
 */


class Mail
{
    //自加属性给Catch用
    public $errorInfo;
    //发送方法 参数一：收件邮箱；参数二：内容。
    public function send($address,$content){
        $mail = new PHPMailer(true);                              // Passing `true` enables exceptions
        try {
            $mail->CharSet = 'UTF-8';//设置收邮件不乱码
            //Server settings
//       $mail->SMTPDebug = 2;                                 // Enable verbose debug output
            $mail->isSMTP();                                      // Set mailer to use SMTP
            $mail->Host = 'smtp.163.com';  // Specify main and backup SMTP servers
            $mail->SMTPAuth = true;                               // Enable SMTP authentication
            $mail->Username = 'zjwz201359@163.com';                 // SMTP username
            $mail->Password = 'x123456';                           // SMTP password  ****授权码 请看下文****
            $mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
            $mail->Port = 994;                                    // TCP port to connect to

            //Recipients
            $mail->setFrom('zjwz201359@163.com', 'Mr.Xie');//发送者
//       $mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
            $mail->addAddress($address);               // Name is optional Name是可选项 发送给谁
//       $mail->addReplyTo('info@example.com', 'Information');//回复不需要
//       $mail->addCC('cc@example.com');
//       $mail->addBCC('bcc@example.com');

            //Attachments 附件不需要
//       $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
//       $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

            //Content  发送内容
            $mail->isHTML(true);                                  // Set email format to HTML内容是否可以支持Html
            $mail->Subject = 'Mr.Xie邮箱验证';//项目标题
            $mail->Body    = $content;//具体内容
//       $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';//附加信息不需要

            $mail->send();
//       echo 'Message has been sent';
            return true;//自加
        } catch (Exception $e) {
//       echo 'Message could not be sent.';
//       echo 'Mailer Error: ' . $mail->ErrorInfo;
            $this->errorInfo = $mail->ErrorInfo;
            return false;//自加
        }
    }
}